package com.qy.controller.manage;

import com.github.pagehelper.PageHelper;
import com.qy.base.core.*;
import com.qy.base.utils.DateUtil;
import com.qy.model.*;
import com.qy.service.*;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by zaq on 2018/07/10.
 */
@RestController("Manage-ClueController")
@RequestMapping("/manage/clue")
public class ClueController {
    @Resource
    private ClueService clueService;

    @Resource
    private PolicyService policyService;
    @Resource
    private PolicyOrderService policyOrderService;
    @Resource
    private UnitService unitService;
    @Resource
    private ChargeService chargeService;

    @PostMapping("/add")
    public Result add(@RequestBody Clue clue) {
        clueService.save(clue);
        return ResultGenerator.successResult();
    }

    @PostMapping("/delete")
    public Result delete(@RequestBody Integer id) {
        clueService.deleteById(id);
        return ResultGenerator.successResult();
    }

    @PostMapping("/update")
    public Result update(@RequestBody Clue clue) {
        clueService.update(clue);
        return ResultGenerator.successResult();
    }

    @GetMapping("/detail")
    public Result detail(@RequestParam Integer id) {
        Clue clue = clueService.findById(id);
        return ResultGenerator.successResult(clue);
    }

    @GetMapping("/list")
    public Result list(PageBean<Clue> page) {
        PageHelper.startPage(page.getPageNum(), page.getSize());
        List<Clue> list = clueService.findAll();
        page.setList(list);
        return ResultGenerator.successResult(page);
    }
    //线索列表
    @GetMapping("clueList")
    public Result clueList(PageBean<Map> page,Integer type ,String content ,Clue clue){
        if (type == 1){
            clue.setUser_name(content);
        }else if (type == 2){
            clue.setUser_tel(content);
        }
        PageHelper.startPage(page.getPageNum(), page.getSize(),"add_time desc");
        List<Map> list = clueService.clueList(clue);
        page.setList(list);
        return ResultGenerator.successResult(page);
    }

    //线索列表-查看线索
    @PostMapping("/seeClue")
    public Result seeClue(@RequestBody Clue clue){
        if (clue.getId() == null){
            throw new ServiceException(Constants.CODE_ERR_PARAM_ID);
        }
        Clue clue1 = clueService.seeClue(clue.getId());
        Map<String,Object> map = new HashMap<>();
        map.put("clue",clue1);
        if (clue1.getCheck_state() == 2){
            List<Map<String,Object>> maps = null;
            if (clue1.getClue_type() ==1){
                maps =policyService.selectByClueId(clue.getId());

            }else if (clue1.getClue_type() ==2){
                maps =unitService.selectByClueId(clue.getId());

            }else if (clue1.getClue_type() ==3){
                maps =chargeService.selectByClueId(clue.getId());
            }

            if (maps.size() >0){
//                Integer ids = (Integer) maps.get(0).get("id");
                Integer ids = Integer.valueOf(maps.get(0).get("id").toString());
//                int id = Integer.parseInt(ids);
                List<Map> policyOrderMapList = policyOrderService.profit( ids,clue1.getClue_type());
                if (policyOrderMapList.get(0).get("profit") == null){
                    map.put("profit",0);
                }else {
                    map.put("profit",policyOrderMapList.get(0).get("profit"));
                }
                map.put("buy_number", policyOrderMapList.get(0).get("buy_number"));
                map.put("brower_number",maps.get(0).get("brower_number"));
            }else {
                map.put("profit", 0);
                map.put("buy_number", 0);
                map.put("brower_number", 0);
            }

        }
        return ResultGenerator.successResult(map);
    }

    //线索列表-审核线索
    @PostMapping("/checkClue")
    public Result checkClue(@RequestBody Clue clue){
        if (clue.getId() == null){
            throw new ServiceException(Constants.CODE_ERR_PARAM_ID);
        }
        if (clue.getCheck_state() == null){
            throw new ServiceException(Constants.CODE_ERR_PARAM);
        }
        clueService.checkClue(clue);
        Clue clue1 = clueService.findById(clue.getId());
        String the_time = String.valueOf(new Date().getTime());
        if (clue.getCheck_state() == 2){
            if (clue1.getClue_type() == 1){
                Policy policy = new Policy();
                policy.setUser_id(clue1.getUser_id());
                policy.setClue_id(clue1.getId());
                policy.setPolicy_name(clue1.getPolicy_name());
                policy.setProvince(clue1.getProvince());
                policy.setCity(clue1.getCity());
                policy.setCounty(clue1.getCounty());
                policy.setSubsidies(clue1.getSubsidies());
                policy.setOther_support(clue1.getOther_support());
                policy.setRelease_unit(clue1.getUnit_name());
                policy.setHold_office(clue1.getPosition());
                policy.setTel(clue1.getTel());
                policy.setName(clue1.getName());
                policy.setSign_condition(clue1.getSign_condition());
                policy.setAudit_process(clue1.getAudit_process());
                policy.setPublisher(clue1.getUser_name());
                policy.setAdd_time(the_time);
                policyService.save(policy);
            }else if (clue1.getClue_type() == 2){
                Unit unit = new Unit();
                unit.setUser_id(clue1.getUser_id());
                unit.setClue_id(clue1.getId());
                unit.setUnit_name(clue1.getUnit_name());
                unit.setCharge_name(clue1.getName());
                unit.setBusiness_scope(clue1.getBusiness_scope());
                unit.setPublisher(clue1.getUser_name());
                //问题
                unit.setProvince(clue1.getProvince());
                unit.setCity(clue1.getCity());
                unit.setCounty(clue1.getCounty());
                unit.setUnit_tel(clue1.getTel());
                unit.setUrl(clue1.getWeb_address());
                unit.setAdd_time(the_time);
                unitService.save(unit);
            }else if (clue1.getClue_type() == 3){
                Charge charge = new Charge();
                charge.setUser_id(clue1.getUser_id());
                charge.setClue_id(clue1.getId());
                charge.setCharge_name(clue1.getName());
                charge.setUnit_name(clue1.getUnit_name());
                charge.setBusiness_scope(clue1.getBusiness_scope());
                charge.setOffice_tel(clue1.getTel());
                charge.setHour(clue1.getHour());
                charge.setMinute(clue1.getMinute());
                charge.setSecond(clue1.getSecond());
                charge.setPublisher(clue1.getUser_name());
                charge.setAdd_time(the_time);
                chargeService.save(charge);

            }
        }
        return ResultGenerator.successResult();
    }
}
